/*
 * Copyright 2018-present KunMinX
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.nsgf.library.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.ListAdapter;
import androidx.recyclerview.widget.RecyclerView;

import com.nsgf.library.R;
import com.nsgf.library.data.bean.DeviceOpeningBean;
import com.nsgf.library.data.bean.HomeDeviceOpeningBean;
import com.nsgf.library.data.bean.UserInfo;
import com.nsgf.library.databinding.ItemDeviceStatusBinding;
import com.nsgf.library.databinding.ItemDeviceStatusGateBinding;

/**
 * 设备状态适配器（根据 facilityType 使用不同布局）
 */
public class DeviceStatusAdapter extends ListAdapter<DeviceOpeningBean.Devices, RecyclerView.ViewHolder> {

    private static final int TYPE_NORMAL = 0; // 普通设备布局
    private static final int TYPE_GATE_GROUP = 1; // 闸门组布局

    private Context mContext;

    public DeviceStatusAdapter(Context context) {
        super(new DiffUtil.ItemCallback<DeviceOpeningBean.Devices>() {
            @Override
            public boolean areItemsTheSame(@NonNull DeviceOpeningBean.Devices oldItem, @NonNull DeviceOpeningBean.Devices newItem) {
                return oldItem.getDeviceCode() != null && oldItem.getDeviceCode().equals(newItem.getDeviceCode());
            }

            @Override
            public boolean areContentsTheSame(@NonNull DeviceOpeningBean.Devices oldItem, @NonNull DeviceOpeningBean.Devices newItem) {
                return (oldItem.getDeviceCode() != null ? oldItem.getDeviceCode().equals(newItem.getDeviceCode()) : newItem.getDeviceCode() == null) &&
                        (oldItem.getDuration() != null ? oldItem.getDuration().equals(newItem.getDuration()) : newItem.getDuration() == null) &&
                        (oldItem.getOpening() != null ? oldItem.getOpening().equals(newItem.getOpening()) : newItem.getOpening() == null);
            }
        });
        this.mContext = context;
    }

    /**
     * 判断是否是 water.obj.type.3 类型
     */
    private boolean isWaterObjType3() {
        return "water.obj.type.3".equals(UserInfo.getInstance().getFacilityType());
    }

    @Override
    public int getItemViewType(int position) {
        // 根据全局 facilityType 决定使用哪种布局
        return isWaterObjType3() ? TYPE_GATE_GROUP : TYPE_NORMAL;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(mContext);
        if (viewType == TYPE_GATE_GROUP) {
            ItemDeviceStatusGateBinding binding = DataBindingUtil.inflate(
                    inflater, R.layout.item_device_status_gate, parent, false);
            return new GateGroupViewHolder(binding);
        } else {
            ItemDeviceStatusBinding binding = DataBindingUtil.inflate(
                    inflater, R.layout.item_device_status, parent, false);
            return new NormalDeviceViewHolder(binding);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof GateGroupViewHolder) {
            // 闸门组布局：使用前5个设备
            DeviceOpeningBean.Devices device = getItem(position);
            ((GateGroupViewHolder) holder).binding.setGate(device);
            ((GateGroupViewHolder) holder).binding.executePendingBindings();
        } else if (holder instanceof NormalDeviceViewHolder) {
            // 普通布局：使用当前设备
            DeviceOpeningBean.Devices device = getItem(position);
            ((NormalDeviceViewHolder) holder).binding.setVm(device);
            ((NormalDeviceViewHolder) holder).binding.executePendingBindings();
        }
    }

    static class NormalDeviceViewHolder extends RecyclerView.ViewHolder {
        ItemDeviceStatusBinding binding;

        NormalDeviceViewHolder(ItemDeviceStatusBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }
    }

    static class GateGroupViewHolder extends RecyclerView.ViewHolder {
        ItemDeviceStatusGateBinding binding;

        GateGroupViewHolder(ItemDeviceStatusGateBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }
    }
}

